// pages/searchGood/index.js
import { searchProduct, getProductHot} from '../../api/api.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    parameter: {
      'navbar': '1',
      'return': '1',
      'title': '搜索商品',
      'color': false
    },
    host_product:[],
    searchValue:'',
    focus:true,
    searchList:[],
    first: 0,
    page:{
      pageNum: 1,
      pageSize: 3
    },
    loading:false,
    loadend:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  getProductList:function(){
    console.log(this.data);
    var that = this;
    if(this.data.loading) return;
    if(this.data.loadend) return;
    console.log(that.data.searchValue.length);
    if(that.data.searchValue.length==0){
      return app.Tips({
          title: '请输入要搜索的商品',
          icon: 'none',
          duration: 1000,
          mask: true,
        });
    }
    this.setData({loading:true,loadTitle:'正在搜索'});
    searchProduct({
      condition:{
        name: that.data.searchValue
      },
      page:that.data.page
    }).then(res=>{
      wx.hideLoading();
      var loadend = res.page.pageNum >= res.page.pages;
      console.log(res);
      that.setData({
        searchList: res.data,
        loading: false,
        loadend: loadend,
        page: {pageNum: res.page.pageNum+1,pageSize: res.page.pageSize},
        loadTitle: loadend ? '已全部加载' : '加载更多',
      });
    }).catch(err=>{
      wx.hideLoading();
      that.setData({ loading: false, loadTitle: "加载更多" });
    });
  },
  getHostProduct: function () {
    var that = this;
    getProductHot().then(res=>{
      that.setData({ host_product: res.data });
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  }, 
  setValue: function (event){
    this.setData({ searchValue: event.detail.value});
  },
  searchBut:function(){
    var that = this;
    if (that.data.searchValue.length > 0){
      that.setData({ page: {pageNum: 1,pageSize: 3}, loadend: false, searchList:[]});
      wx.showLoading({ title:'正在搜索中'});
      that.getProductList();
    }else{
      return app.Tips({
        title: '请输入要搜索的商品',
        icon: 'none',
        duration: 1000,
        mask: true,
      });
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getHostProduct();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getProductList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})